home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
General
/
CIncludesTool
/
original version
/
source files
/
reformat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-29
|
3KB
|
145 lines
#include "CIncludesCode.h"
#include <Memory.h>
#include <OSUtils.h>
#include <StdIO.h>
#include <ToolUtils.h>
extern Handle database;
extern dataHeaderType dataHeader;
Handle textHdl;
Handle arrayHdl;
long includeOffset;
short findNameIndex( char *s )
{
short i;
strArray *sp = (strArray*) (*database + dataHeader.nameArrayOffset);
for ( i = 0; i < dataHeader.numFiles; ++i )
if ( equalStringsUncased( s, (*sp)[i]) )
return i;
return -1;
}
long removeLine( long pos )
{
long p;
char *dp = StripAddress( *textHdl );
while ( (pos > 0) && (*(dp + pos - 1) != '\n') )
--pos;
p = pos;
while ( *(dp + p) != '\n' ) ++p;
return Munger( textHdl, pos, 0, (p - pos + 1), dp, 0 );
}
void extract()
{
short fileIndex;
long pos = 0;
long limit = GetHandleSize( textHdl );
char s[256];
includeOffset = -1;
while ( (pos = nextInclude( textHdl, pos, limit )) < limit )
{
extractFileName( s, *textHdl + pos );
if ( (fileIndex = findNameIndex( s )) != -1 )
{
*( *arrayHdl + fileIndex ) = 1;
pos = removeLine( pos );
if ( includeOffset == -1 )
includeOffset = pos;
limit = GetHandleSize( textHdl );
}
}
}
void prune()
{
short i, j;
char *p = StripAddress( *arrayHdl ); // dereference
for ( i = 0; i < dataHeader.numFiles; ++i )
if ( *(p + i) )
for ( j = 0; j < dataHeader.numFiles; ++j )
if ( *(p + j) &&
(i != j) &&
isDependent( *database + dataHeader.dependencyArrayOffset, j, i ) )
{
*(p + i) = 0;
break;
}
}
void reinsert()
{
short i;
char s[256];
for ( i = 0; i < dataHeader.numFiles; ++i )
if ( *( *arrayHdl + i ) )
{
sprintf( s, "#include <%s>\n", getNamePtr( i ) );
includeOffset = Munger( textHdl, includeOffset, 0, 0, s, strlen( s ) );
}
}
int reformat( char *src )
{
FILE *f = fopen( src, "r" );
long len;
if ( !f ) errorExit( "Could not open selection" );
(void) fseek( f, 0, SEEK_END );
if ( !(len = ftell( f )) )
{
SysBeep(1);
return 0;
}
textHdl = NewHandle( len );
checkMemory();
arrayHdl = NewHandleClear( dataHeader.numFiles );
checkMemory();
(void) fseek( f, 0, SEEK_SET );
HLock( textHdl );
if ( fread( *textHdl, 1, len, f ) != len )
errorExit( "Read selection failed" );
HUnlock( textHdl );
if ( *(*textHdl + len - 1) != '\n' )
{
(void) Munger( textHdl, len, 0, 0, "\n", 1);
checkMemory();
}
extract();
prune();
reinsert();
f = freopen( src, "w", f );
HLock( textHdl );
(void) fwrite( *textHdl, 1, GetHandleSize( textHdl ), f );
DisposHandle( textHdl );
DisposHandle( arrayHdl );
return 0;
}